package com.example.managemyself.ui.tasks;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.example.managemyself.R;
import com.example.managemyself.ui.tasks.AddEditTaskActivity;
import com.example.managemyself.util.DateManager;
import com.example.managemyself.viewmodel.TaskViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.Date;

/**
 * 任务管理主Fragment
 * 包含学习、健康、杂务三个标签页面
 */
public class TasksFragment extends Fragment implements DateManager.DateChangeListener {

    private TaskViewModel taskViewModel;
    private DateManager dateManager;
    
    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private TaskPagerAdapter pagerAdapter;
    private FloatingActionButton fabAddTask;
    
    private final String[] tabTitles = {"学习", "健康", "杂务"};

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_tasks, container, false);
        
        initViews(root);
        return root;
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 初始化ViewModel
        taskViewModel = new ViewModelProvider(requireActivity()).get(TaskViewModel.class);
        
        // 初始化日期管理器
        dateManager = DateManager.getInstance();
        dateManager.addDateChangeListener(this);
        
        setupViewPager();
        setupFab();
    }
    
    private void initViews(View root) {
        tabLayout = root.findViewById(R.id.tabLayout);
        viewPager = root.findViewById(R.id.viewPager);
        fabAddTask = root.findViewById(R.id.fabAdd);
    }
    
    private void setupViewPager() {
        // 创建适配器
        pagerAdapter = new TaskPagerAdapter(this, tabTitles.length);
        viewPager.setAdapter(pagerAdapter);
        
        // 关联TabLayout和ViewPager2
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            tab.setText(tabTitles[position]);
        }).attach();
        
        // 设置ViewPager变化监听器
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                updateFabForCurrentTab(position);
            }
        });
    }
    
    private void setupFab() {
        if (fabAddTask != null) {
            fabAddTask.setOnClickListener(v -> {
                Intent intent = new Intent(requireContext(), AddEditTaskActivity.class);
                
                // 根据当前选中的标签页设置默认任务类型
                int currentTab = viewPager.getCurrentItem();
                if (currentTab >= 0 && currentTab < tabTitles.length) {
                    intent.putExtra("default_type", tabTitles[currentTab]);
                }
                
                startActivity(intent);
            });
        }
    }
    
    private void updateFabForCurrentTab(int position) {
        // 可以根据不同的标签页调整FAB的显示或行为
        if (fabAddTask != null) {
            // 所有标签页都显示添加按钮
            fabAddTask.show();
        }
    }
    
    @Override
    public void onDateChanged(Date newDate) {
        // 日期变化时，通知所有子Fragment刷新数据
        // 子Fragment会通过DateManager自动接收到变化通知
    }
    
    @Override
    public void onResume() {
        super.onResume();
        // 页面恢复时刷新数据
        if (pagerAdapter != null) {
            pagerAdapter.notifyDataSetChanged();
        }
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dateManager != null) {
            dateManager.removeDateChangeListener(this);
        }
    }
} 